package dao

import (
	"219yk/inits"
	"219yk/models"
)

func CreateRoom(userId int) bool {
	room := models.Room{
		RoomUserId: userId,
	}
	err := inits.DB.Debug().Create(&room).Error
	if err != nil {
		return false
	}

	user := models.RoomUser{
		RoomUserId: room.RoomUserId,
		UserId:     userId,
		RoomId:     int(room.ID),
		ULevel:     1,
	}
	err = inits.DB.Debug().Create(&user).Error
	if err != nil {
		return false
	}
	return true
}

func RoomGet(id int) bool {
	var room models.RoomUser
	err := inits.DB.Debug().Where("room_id = ?", id).Limit(1).Find(&room).Error
	if err != nil {
		return false
	}
	return true
}

func UserGet(id []int) bool {
	for _, i2 := range id {
		var user models.User
		err := inits.DB.Debug().Where("id = ?", i2).Find(&user).Error
		if err != nil {
			return false
		}
	}
	return true

}

func GetULeveL(userid int) models.RoomUser {
	var user models.RoomUser
	err := inits.DB.Debug().Where("user_id = ?", userid).Limit(1).Find(&user).Error
	if err != nil {
		return models.RoomUser{}
	}
	return user
}

func CreateRoomUser(userroom models.RoomUser) bool {
	err := inits.DB.Debug().Create(&userroom).Error
	if err != nil {
		return false
	}
	return true
}

func UpdateUlevel(ulevel models.RoomUser) bool {

	err := inits.DB.Debug().Update("u_level", &ulevel).Error
	if err != nil {
		return false
	}
	return true
}

func DelRoom(id int) bool {
	room := models.Room{}
	err := inits.DB.Debug().Model(&models.Room{}).Where("id = ?", id).Delete(&room).Error
	if err != nil {
		return false
	}
	return true
}
